package edu.lsnu.equipment_management_system.interceptor;

import edu.lsnu.equipment_management_system.pojo.vo.out.session.UserVo;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author mzy
 * @Description 自定义拦截器；拦截普通用户相关操作的；拦截之后返回到普通用户登陆页面
 * @date 2018/10/31
 */
public class UserLoginInterceptor implements HandlerInterceptor {
    /**
     * @Description 进入Controller方法之前
     * @author mzy
     * @time 2018-11-03
     * @see
     * @return   boolean
     * @params  [request, response, handler]
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        HttpSession session = request.getSession();
        UserVo user = (UserVo) session.getAttribute("user");
        if (null == user) {
            response.sendRedirect("/login/user.html");

            return false;
        }

        return true;
    }

    /**
     * @Description 调用完Controller之后，视图渲染之前
     * @author mzy
     * @time 2018-11-03
     * @see
     * @return   void
     * @params  [request, response, handler, modelAndView]
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {

        // System.out.println("loginInterceptor -> posthandle");
    }

    /**
     * @Description 整个过程完成之后，通常用于资源清理
     * @author mzy
     * @time 2018-11-03
     * @see
     * @return   void
     * @params  [request, response, handler, ex]
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {

        // System.out.println("loginInterceptor -> afterCompletion");
    }
}
